//
//  CPHomeCell.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/4.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPHomeCell.h"

@interface CPHomeCell()
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *authorLabel;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) UIView *view;
@end

@implementation CPHomeCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
        view.backgroundColor = [UIColor grayColor];
        view.alpha = 0.2;
        [self.contentView addSubview:view];
        self.view = view;
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont systemFontOfSize:18.0];
        titleLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UILabel *authorLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        authorLabel.textAlignment = NSTextAlignmentLeft;
        authorLabel.font = [UIFont systemFontOfSize:14.0];
        authorLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:authorLabel];
        self.authorLabel = authorLabel;
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.font = [UIFont systemFontOfSize:16.0];
        contentLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:contentLabel];
        self.contentLabel = contentLabel;
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat mar = 10;
    CGFloat h = (self.height-mar*2)/3;
    _titleLabel.frame = CGRectMake(10, mar, self.width*0.7, h);
    _authorLabel.frame = CGRectMake(10, CGRectGetMaxY(_titleLabel.frame), self.width-30, h);
    _contentLabel.frame = CGRectMake(10, CGRectGetMaxY(_authorLabel.frame), self.width-20, h);
    _view.frame = CGRectMake(10, self.height -1, self.width-20, 1);
}

- (void)setModel:(CPModel *)model
{
    _model = model;
    _titleLabel.text = model.cp_title;
    if (model.cp_dynasty) {
        _authorLabel.text = [NSString stringWithFormat:@"%@ / %@", model.cp_dynasty, model.cp_author];
        NSArray *content = [model.cp_content componentsSeparatedByString:@"\n"];
        _contentLabel.text = [NSString stringWithFormat:@"%@，%@。",content[3],content[4]];
    }else{ // 名句分享
        _authorLabel.text = [NSString stringWithFormat:@"作者: %@ / 类型: %@", model.cp_author, model.cp_type];
        NSArray *content = [model.cp_content componentsSeparatedByString:@"\n"];
        _contentLabel.text = [NSString stringWithFormat:@"%@，%@。",content[2],content[3]];
    }
    self.alpha = 0.0;
    JMSelf(ws);
    [UIView animateWithDuration:0.4 animations:^{ws.alpha = 1.0;}];
}
@end
